/*<div class="zs-list">
				<p>多得多地点多多多多</p>				
				<span class="fa fa-tag float-right sc"></span>
				<span class="float-right">公益</span>
				<div class="pic">
					<a href="#"><img src="<%=path %>/wap/img/iu.jpg" alt="" /></a>
				</div>
				<span class="s-title">打发打发</span>
				<span class="fa fa-heart-o sc float-left"></span>
				<span class="float-left zs-ll">100</span>
				<span class="fa fa-eye sc float-left"></span>
				<span class="float-left">100</span>
</div>*/
//计算汉字长度的方法
var requestFlag = 0;
var jmz = {};
jmz.GetLength = function(str) {
  return str.replace(/[\u0391-\uFFE5]/g,"aa").length;  //先把中文替换成两个字节的英文，在计算长度
};
var pageSize = 8;
//当前页面载入
$("document").ready(function(){
	//$(".zz-list").remove();
	$(".jz-data").css("display","block");
	//pageSize = $("#pageNo").val()*8;
	//alert($("#pageNo").val()+"D");
	$("#pageNo").val(1);
	if (name!=null && name!='') {
		$("#planName").val(name);
	}
	getDemo();
	$(window).scroll(function(){
    	if($(window).scrollTop() + $(window).height() >= documentHeight && ff){   
    		ff=false;
    		pageChange();
    	}
    })
})

var documentHeight = 0;
//分页的触发事件
function pageChange(){
	var pn = Number($("#pageNo").val());
	pn+=1;
	$("#pageNo").val(pn);
	getDemo();
	
}
//搜索框搜索
function search(){
	$("#pgNo").val(1);
	$(".jz-data").css("display","block");
	$(".js-list div").removeClass("chose");
	$("#industies div").eq(0).addClass("chose");
	$("#holidays div").eq(0).addClass("chose");
	$("#functions div").eq(0).addClass("chose");
	$(".sort").eq(0).addClass("chose");
	$(".zwsj").remove();
	$(".zzz-list").remove();
	$('.ss-tc').removeClass('ss-tc-show').addClass('ss-tc-hide');
	getDemo();
}

//请求数据
function getDemo(){
	var planN = $("#planName").val();
	var planT = '';
	var industies = $("#industies .chose").attr("code");
	var holidays = $("#holidays .chose").attr("code");
	var functions = $("#functions .chose").attr("code");
	var pgNo=$("#pageNo").val();
	var pgSize = 8;
	var orderBy = $(".order-by .chose").attr("code");
	planT = industies+','+holidays+','+functions;
	if (requestFlag==0) {
		requestFlag = 1;
		$.ajax({
			type:"GET",
			url:basePath+"/wap/cases.html",
			data:{planName:planN,planTag:planT,pageNo:pgNo,pageSize:pgSize,orderBy:orderBy},
			dataType:"json",
			success:function(data){
				var map = eval(data);
				var count = data.count;
				var list = data.list;
				buildCases(list);
				documentHeight=$(document).height();
				count = Math.ceil(count/8);
				if (count<=pgNo) {
					$(".jz-data").css("display","none");
					$(window).unbind('scroll');
				}
				requestFlag = 0;
				ff=true;
				//alert(pgNo);
			},
			error:function(){
				alert("error");
			}
		});
	}
}
//构建数据
function buildCases(data){
	var json = data;
	var html = "";
	$.each(
			json,
			function(i,item){
				var lengt = (item.planTag).split(",");
				var tag = "";
				for (var i = 0; i < lengt.length; i++) {
					if (lengt[i] == '') {
						lengt[i]=" ";
					}
					tag+=lengt[i]+'&nbsp; ';
				}
				var name = item.planName;
				if (jmz.GetLength(name)>18) {
					name = (item.planName).substr(0,9)+"...";
				}
				/*var like = parseFloat(item.planHot);
				if (like>10000) {
					like = Math.round(like/10000)+"W";
				}
				var watch = parseFloat(item.planWatch);
				if (watch>10000) {
					watch = Math.round(watch/10000)+"W";
				}*/
				html+="<div class='zs-list zzz-list'>"
				+"<p>"+name+"</p>"
				+"<span class='fa fa-tag float-right sc'></span>"
				+"<span class='float-right'>"+tag+"</span>"
				+"<div class='pic'><a href='"+basePath+"/detail/show.html?id="+item.planId+"'>"
				+"<img src='"+basePath+"/file/getImg.html?saveAddress="+item.planCover+"' alt='' /></a></div>"
				+"<span class='s-title'>"+name+"</span>"
				+"<span id='id' style='display:none'>"+item.planId+"</span>";
				if (item.planHot>0) {
					html+="<span class='fa fa-heart sc float-l zan' style='color:red'></span>";
				}else{
					html+="<span class='fa fa-heart-o sc float-l zan'></span>";
				}
				html+="<span class='float-l zs-ll zan no'>"+item.planHot+"</span>"
				+"<span class='float-r'>"+item.planWatch+"</span>"
				+"<span class='fa fa-eye sc float-r'></span></div>"; 
				
			}
	)
	if (json.length==0) {
		html="<div class='zwsj'>暂无数据</div>"
	}
	$("#cases").append(html);
	bindClick();
	var shuH=$('.zs-list .pic').width()*0.714;
	$('.zs-list .pic img').height(shuH);
}
function bindClick(){
	//点赞
	$(this).addClass('fa-heart').removeClass('fa-heart-o');
	/*var gzText=parseInt($('.gz').text());
	$('.gz').text(1+gzText);*/
	$(".zan").click(function(){
		var _this =  $(this);
		var planId = _this.parent().find("#id").text();
		var count = _this.parent().find(".zan.no").text();
		var num = Number(count);
		if (!num>0) {
			_this.parent().find(".fa-heart-o").addClass("fa-heart").css("color","red").removeClass("fa-heart-o");
		}
		$.ajax({
				type:"POST",
				url:basePath+"/front/addLike.html",
				data:{planId:planId},
				dataType:"json",
				success:function(json){
					_this.parent().find(".zan.no").text(num+1);
					if (json==1) {
						layer.open({
							content: '点赞成功！'
								,skin: 'msg'
									,time: 1 //2秒后自动关闭
						});
					}else{
						layer.open({
							content: '点赞失败！'
								,skin: 'msg'
									,time: 1 //2秒后自动关闭
						});
					}
				},
				error:function(){
					layer.open({
					    content: '点赞失败！'
					    ,skin: 'msg'
					    ,time: 1 //2秒后自动关闭
					  });
				}
		});
	})
}
